﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	Print
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace Map_Fvg
{
    public partial class Print : Form
    {
 
        bool bLandscape;
        int multi_pages;

        public Print()
        {
            InitializeComponent();
        }

        static int comboBox2_InitialValue = 0;

        private void Form1_Load(object sender, EventArgs e)
        {
            String pkInstalledPrinters;
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
                comboBox1.Items.Add(pkInstalledPrinters);
            }

            comboBox1.Text = PrinterSettings.InstalledPrinters[0];
            comboBox1.Update();

            textBox1.Text = "5000";
            string ss = " A4 : 210 x 297";
            comboBox2.Items.Add(ss);
            string ss1 = " A3 : 297 x 410 ";
            comboBox2.Items.Add(ss1);
            string ss2 = " A2 : 410 x 584";
            comboBox2.Items.Add(ss2);
            string ss3 = " A1 : 584 x 831 ";
            comboBox2.Items.Add(ss3);


            comboBox2.Text = ss;
            comboBox2_InitialValue = 1;

            bLandscape = true;
            multi_pages = 0;

            string s2 = "0";
            string s3 = width1.ToString();
            string s4 = "0";
            string s5 = height1.ToString();

            textBox2.Text = s2;
            textBox3.Text = s3;
            textBox4.Text = s4;
            textBox5.Text = s5;

            initX = 0;
            initY = 0;

        }

        private void printButton_Click(object sender, EventArgs e)
        {
            
            if (checkBox3.CheckState == CheckState.Checked)
            {
                {
                    OpenFileDialog dlg = new OpenFileDialog();
                    dlg.CheckFileExists = false;
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        string file = dlg.FileName;
                        string ss = comboBox2.Text;
                        int ipos = ss.LastIndexOf('A');
                        string sizeP = ss.Substring(ipos, 2 );
                        drawPdf(file, sizeP, bLandscape);
                    }
                }
                return;
            }
            
            //       try
            {
                PrintDocument pd = new PrintDocument();

                pd.PrintPage += new PrintPageEventHandler (this.pd_PrintPage);
                pd.PrinterSettings.PrinterName = comboBox1.Text;
                pd.DefaultPageSettings.Landscape = bLandscape;
                pd.Print();
            }
        }

        // The PrintPage event is raised for each page to be printed. 
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {

            drawMap(ev);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        public static int initX = 0, initY = 0;
        static int width1, height1, orizz, ivert, shiftX, shiftY;
        int Xoff, Yoff, Yoff1;
        int RGxx;
        int RGyy;

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (ViewQ.readViewQ == 0) return;

            Point RGmin = new Point();
            Point RGmax = new Point();

            RGmin.X = (2 * this.panel1.Size.Width) / 100;
            RGmin.Y = (2 * this.panel1.Size.Height) / 100;
            RGmax.X = (98 * this.panel1.Size.Width) / 100;
            RGmax.Y = (98 * this.panel1.Size.Height) / 100;


            int Xdiff = ViewQ.Xmax - ViewQ.Xmin;
            int Ydiff = ViewQ.Ymax - ViewQ.Ymin;

            float scalaX = (float)(RGmax.X - RGmin.X) / (float)Xdiff;
            float scalaY = (float)(RGmax.Y - RGmin.Y) / (float)Ydiff;
            float scalaZ;
            if (scalaX < scalaY) scalaZ = scalaX;
            else scalaZ = scalaY;

            scalaX = scalaZ;
            scalaY = -scalaZ;
            RGxx = RGmax.X - RGmin.X;
            RGyy = RGmax.Y - RGmin.Y;
 
            Graphics dc = this.panel1.CreateGraphics();
            dc.Clear(Color.White);

            Pen blackPen = new Pen(Color.Black, 1);

            Xoff = 0;// (RGmax.X + RGmin.X - (int)(Xdiff * scalaX)) / 2;
            Yoff = RGmax.Y;  // -(int)(Ydiff * scalaX / 2);
            Yoff1 = RGmax.Y + RGmin.Y - (int)(Ydiff * scalaX) - 15;

            Rectangle Border = new Rectangle(Xoff, Yoff - (int)(Ydiff * scalaX), (int)(Xdiff * scalaX), (int)(Ydiff * scalaX));

            dc.DrawRectangle(blackPen, Border);
            //
            //      line_shp
            //
            dc.RotateTransform(ViewQ.alpha);

            for (int i = 0; i < ViewQ.nShapes; ++i)  //    nShapes; ++i)
            {
                int nVertices = ViewQ.vectShapes[i].nVertices;
                int ipnt = ViewQ.vectShapes[i].pntpL;
                int pntSimb = ViewQ.vectShapes[i].pntSimb;
                bool bDraw = false;
                if (ViewQ.vectS[pntSimb].codice.Contains("1L000CD")) bDraw = true;
                if (ViewQ.vectS[pntSimb].codice.Contains("1L000DI")) bDraw = true;
                if (ViewQ.vectS[pntSimb].codice.Contains("5L000SI")) bDraw = true;
                if (ViewQ.vectS[pntSimb].codice.Contains("5L000SE")) bDraw = true;
                if(!bDraw) continue;
                if (ViewQ.vectS[pntSimb].selected == 0) continue;
                if(ViewQ.vectS[pntSimb].visib == 0) continue;

                Color icolor = ViewQ.vectS[pntSimb].icolor;
                float width = ViewQ.vectS[pntSimb].penWidth;

                Pen iPen = new Pen(icolor, width);

                iPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;

                Point[] poly = new Point[nVertices];
                for (int j = 0; j < nVertices; ++j)
                {
                    poly[j].X = (int)((ViewQ.PL[ipnt].X - ViewQ.Xmin) * scalaX) + Xoff - 2;
                    poly[j].Y = (int)((ViewQ.PL[ipnt].Y - ViewQ.Ymin) * scalaY) + Yoff + 4;
                    ++ipnt;
                }
                dc.DrawCurve(iPen, poly);
            }

            dc.ResetTransform();

            char[] delimiterChars0 = { 'x', '(', ':' };
            string[] words;

            string ss = comboBox2.Text;
            words = ss.Split(delimiterChars0);
            string num1 = words[1];
            string num2 = words[2];

            int inum1 = int.Parse(num1);
            int inum2 = int.Parse(num2);

            orizz = inum2;
            ivert = inum1;
            if (!bLandscape)
            {
                orizz = inum1;
                ivert = inum2;
            }
            shiftX = (int)( Orig_X + Margin_X);
            shiftY = (int)( Orig_Y + Margin_Y);

            ss = textBox1.Text;
            int scalaTextbox1 = int.Parse(ss);

            Pen redPen = new Pen(Color.Red, 3);
            width1 = (int)((float)(orizz - shiftX) * scalaTextbox1 * scalaZ / 1000.0F);
            height1 = (int)((float)(ivert - shiftY) * scalaTextbox1 * scalaZ / 1000.0F);
            Rectangle Cornice = new Rectangle(initX + Xoff, initY + Yoff - (int)(Ydiff * scalaX), width1, height1);

            dc.DrawRectangle(redPen, Cornice);
            this.panel1.Show();

            string s2 = initX.ToString();
            string s3 = (initX + width1).ToString();
            string s4 = initY.ToString();
            string s5 = (initY + height1).ToString();

            textBox2.Text = s2;
            textBox3.Text = s3;
            textBox4.Text = s4;
            textBox5.Text = s5;

        }

 

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true) bLandscape = false;
            else
            {
                bLandscape = true;
            }
            this.panel1.Invalidate();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true) multi_pages = 1;
            else
            {
                multi_pages = 0;
            }

            if (multi_pages == 1)
            {
                MessageBox.Show("opzione non ancora operante");
                checkBox1.Checked = false;
                multi_pages = 0;
            }
        }

        int dragRect, oldeX, oldeY;

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {

            dragRect = 0;
            int eX = e.X - Xoff;
            int eY = e.Y - Yoff1;
            if (initX > eX) return;
            if (initX + width1 < eX) return;
            if (initY + height1 < eY) return;
            if (initY > eY + 1) return;
            dragRect = 1;
            oldeX = eX;
            oldeY = eY;

        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            dragRect = 0;

            string s2 = initX.ToString();
            string s3 = (initX + width1).ToString();
            string s4 = initY.ToString();
            string s5 = (initY + height1).ToString();

            textBox2.Text = s2;
            textBox3.Text = s3;
            textBox4.Text = s4;
            textBox5.Text = s5;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragRect == 0) return;
            int eX = e.X - Xoff;
            int eY = e.Y - Yoff1;
            initX += eX - oldeX;
            initY += eY - oldeY;
            this.panel1.Invalidate();
            oldeX = eX;
            oldeY = eY;
        }

         private void button2_Click(object sender, EventArgs e)
        {
            initX += 1;
            string s2 = initX.ToString();
            string s3 = (initX + width1).ToString();
            textBox2.Text = s2;
            textBox3.Text = s3;

            panel1.Invalidate();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            initX -= 1;
            string s2 = initX.ToString();
            string s3 = (initX + width1).ToString();
            textBox2.Text = s2;
            textBox3.Text = s3;

            panel1.Invalidate();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            initY -= 1;
            string s4 = initY.ToString();
            string s5 = (initY + height1).ToString();
            textBox4.Text = s4;
            textBox5.Text = s5;

            panel1.Invalidate();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            initY += 1; string s4 = initY.ToString();
            string s5 = (initY + height1).ToString();
            textBox4.Text = s4;
            textBox5.Text = s5;
            panel1.Invalidate();
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            this.panel1.Invalidate();

        }

        private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
        {
            if(comboBox2_InitialValue == 0) return;

            panel1.Invalidate();

            if (!comboBox2.Text.Contains("A4")) checkBox3.CheckState = CheckState.Checked;
          
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox3.CheckState != CheckState.Checked)
            {
                string ss = " A4 : 210 x 297 ";
                comboBox2.Text = ss;
                panel1.Invalidate();

            }
        }


    }
}
